import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.GlobalHeaders;
import cn.hutool.http.HttpUtil;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

// 请确保您已将 API Key 存储在环境变量 ARK_API_KEY 中
// 初始化Ark客户端，从环境变量中读取您的API Key
public class LiveDownload {
    public static void main(String[] args) throws IOException {
        if(true&&false){
            System.out.println("x");
        }
        String prefix = "https://www.earthcam.com/api/ectv/player/playlist?r=playlist&a=fetch";

        String url = "https://videos-3.earthcam.com/fecnetwork/13706.flv/chunklist_w5203310391.m3u8?t=z6nB2mvB7fVRwGP9d5q8HkQOt517mPZeS8kGxr9WE4zL6uBd9F64CxKEofFBjCSp&td=202504220425";
//        GlobalHeaders.INSTANCE.header("authority","hd-auth.skylinewebcams.com");
//        GlobalHeaders.INSTANCE.header("path","/live.m3u8?a=e73mka37knl2kb81mlq7k5upa0");
//        GlobalHeaders.INSTANCE.header("accept","*/*");
//        GlobalHeaders.INSTANCE.header("accept-encoding","gzip, deflate, br, zstd");
//        GlobalHeaders.INSTANCE.header("accept-language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6");
//        GlobalHeaders.INSTANCE.header("origin","https://www.skylinewebcams.com");
//        GlobalHeaders.INSTANCE.header("priority","u=1, i");
//        GlobalHeaders.INSTANCE.header("referer","https://www.skylinewebcams.com/");
//        GlobalHeaders.INSTANCE.header("sec-ch-ua","\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"");
//        GlobalHeaders.INSTANCE.header("sec-ch-ua-mobile","?0");
//        GlobalHeaders.INSTANCE.header("sec-ch-ua-platform","Windows");
//        GlobalHeaders.INSTANCE.header("sec-fetch-dest","empty");
//        GlobalHeaders.INSTANCE.header("sec-fetch-mode","cors");
//        GlobalHeaders.INSTANCE.header("sec-fetch-site","same-site");
        GlobalHeaders.INSTANCE.header("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36");

        byte[] bytes1 = HttpUtil.downloadBytes(prefix);
        String add1= new String(bytes1);
        System.out.println(add1);
        byte[] bytes = HttpUtil.downloadBytes(url);
        String add= new String(bytes);
        List<String> urlList = getUrlList(add);
        FileUtil.writeBytes(bytes,"/Users/weixin/work/project/self/square-lake/square-lake-app/src/test/java/test.m3u8");

        Integer i =1;
        for (String s : urlList) {
            url = "https://videos-3.earthcam.com/fecnetwork/13706.flv/"+s;
            bytes = HttpUtil.downloadBytes(url);
            String fileUrl = "/Users/weixin/work/project/self/square-lake/square-lake-app/src/test/java/"+i+".ts";
        FileUtil.del(fileUrl);
            File file = FileUtil.file(fileUrl);
            file.createNewFile();
            FileUtil.writeBytes(bytes,fileUrl);
        i++;
        }

    }
    public static List<String> getUrlList(String url ){
        List<String> split = StrUtil.split(url, "#EXTINF");
        System.out.println(url);

        if(CollUtil.size(split)==1){
return Collections.emptyList();
        }

        List<String> mediaW = split.stream().map(v -> {
            if (!StrUtil.contains(v, "media_w")) {
                return "";
            }
            return StrUtil.split(v, ",").get(1);
        }).map(StrUtil::trim).filter(StrUtil::isNotBlank).collect(Collectors.toList());
        System.out.println(mediaW);
        return mediaW;
    }
}